<template>
  <el-form ref="deptForm" label-width="120px">
    <el-form-item label="部门名称">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {getEmployeeSimple} from '@/api/employees'
import { addDepartments } from '@/api/department'
import { getDepartDetail } from '@/api/department'
import { updateDepartments } from '@/api/department'
export default {
  props:{
    pid:{
      required:true,
      type:String
    },
    isEdit:{
      required:true,
      type:Boolean
    }
  },
  created(){
    this.loadEmployees()
    this.loadDetail()
  },
  methods:{
    hCancel(){
      this.$emit('close')
    },
    async loadDetail(){
      if(this.isEdit){
        
        const res = await getDepartDetail(this.pid)
        console.log('现在是编辑,要去获取详情',res)
        this.form = res.data
      }
    },
    async doAdd() {
      const d = {...this.form,pid:this.pid}
      await addDepartments(d)
      //通知父组件关闭弹层  更新数据
      this.$emit('success')
    },
    async doEdit(){
      await updateDepartments(this.form)
      this.$message({ type: 'success', message: '修改成功' })
      this.$emit('success')
    },
    hSubmit(){
    //表单校验
    //具体做添加
    this.isEdit ? this.doEdit():this.doAdd()
  },
    async loadEmployees(){
      const res = await getEmployeeSimple()
      console.log('res',res)
      this.list = res.data
    }
  },
    data() {
    return {
      list:[],//用来保存所有的员工信息
      form: {
        name: '最新部门', // 部门名称
        code: '123456', // 部门编码
        manager: '', // 部门管理者
        introduce: '部门介绍' // 部门介绍
      }
    }
  }
}
</script>

<style>

</style>